package com.wm.wmim.server.service.impl;


import com.wm.wmim.common.enums.BizCodeEnum;
import com.wm.wmim.common.enums.CommonBizCodeEnum;
import com.wm.wmim.common.exception.BusinessException;
import com.wm.wmim.server.api.bo.resp.UserInfoBO;
import com.wm.wmim.server.dto.bo.req.LoginReqBO;
import com.wm.wmim.server.dao.UserInfoDao;
import com.wm.wmim.server.dbservice.UserInfoDbService;
import com.wm.wmim.server.entity.UserInfoDO;
import com.wm.wmim.server.mapstruct.UserInfoMapstruct;
import com.wm.wmim.server.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (UserInfo)表服务实现类
 *
 * @author weiming
 * @since 2024-08-19 23:18:34
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserInfoDbService userInfoDbService;
    @Resource
    private UserInfoMapstruct userInfoMapstruct;

    @Override
    public UserInfoBO login(LoginReqBO reqBO) {
        UserInfoDO userInfoDO = userInfoDbService.getByUserCode(reqBO.getUserCode());
        if (userInfoDO == null){
            throw new BusinessException(CommonBizCodeEnum.LOGIN_USER_NOT_EXIST);
        }
        String reqPassword = reqBO.getPassword();
        if (!StringUtils.equals(reqPassword, userInfoDO.getPassword())){
            throw new BusinessException(CommonBizCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        return userInfoMapstruct.fromDoToBo(userInfoDO);
    }

    @Override
    public UserInfoBO loginCheck(String userCode) {
        UserInfoDO userInfoDO = userInfoDbService.getByUserCode(userCode);
        // TODO 查询登录信息, 有的话返回true
        UserInfoBO userInfoBO = userInfoMapstruct.fromDoToBo(userInfoDO);
        return userInfoBO;
    }
}

